package taptap

import (
	"batbat-backend/app/model"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
)

/*
获取并解析taptap的游戏的详情
*/

func GetDetail(pair *model.TaptapIdRankPair) (*model.TaptapGameDetail, error) {
	// 获取url, 发起请求
	url := genDetailUrl(pair.TaptapId)
	response, err := g.Client().Get(url)
	if err != nil {
		return nil, gerror.Newf("请求游戏详情失败, err=%v", err)
	}
	if response.StatusCode != 200 {
		return nil, gerror.Newf("请求游戏详情失败, 响应码=%v, taptapId=%v", response.StatusCode, pair.TaptapId)
	}

	// 解析响应体
	var detail *taptapGameDetail
	jsonString := response.ReadAllString()
	err = gjson.DecodeTo(jsonString, &detail)
	if err != nil {
		return nil, gerror.Newf("游戏详情数据分析失败, err=%v, taptapId=%v, json=%v", err, pair.TaptapId, jsonString)
	}
	if !detail.Success {
		return nil, gerror.Newf("游戏详情数据获取失败, json=%v", jsonString)
	}

	// 解析taptapGameDetail为model.TaptapGameDetail对象
	res := &model.TaptapGameDetail{
		Game: &model.Game{
			UpdateTime:  gtime.Now(),
			TaptapId:    uint(pair.TaptapId),
			Name:        detail.Data.Title,
			Description: detail.Data.Description.Text,
			Rank:        pair.Rank,
		},
		Developers: make([]*model.Developer, len(detail.Data.Developers)),
		Categories: make([]*model.Category, len(detail.Data.Tags)),
		Stat: &model.GameStat{
			UpdateTime: gtime.Now(),
			UpdateDate: detail.Data.UpdateDate,
			Download:   detail.Data.Stat.HitsTotal,
			Play:       detail.Data.Stat.PlayTotal,
			Favor:      detail.Data.Stat.FansCount,
		},
		Banner: &model.GameBanner{
			UpdateTime:  gtime.Now(),
			Width:       detail.Data.Banner.Width,
			Height:      detail.Data.Banner.Height,
			MediumUrl:   detail.Data.Banner.MediumUrl,
			OriginalUrl: detail.Data.Banner.OriginalUrl,
		},
		Icon: &model.GameIcon{
			UpdateTime:  gtime.Now(),
			Width:       detail.Data.Icon.Width,
			Height:      detail.Data.Icon.Height,
			MediumUrl:   detail.Data.Icon.MediumUrl,
			OriginalUrl: detail.Data.Icon.OriginalUrl,
		},
		Apk: &model.GameApk{
			TaptapId:    detail.Data.Download.ApkId,
			UpdateTime:  gtime.Now(),
			Name:        detail.Data.Download.Apk.Name,
			Size:        detail.Data.Download.Apk.Size,
			VersionCode: detail.Data.Download.Apk.VersionCode,
			VersionName: detail.Data.Download.Apk.VersionName,
		},
		Screenshots: make([]*model.GameScreenshot, len(detail.Data.Screenshots)),
	}
	// 开发者
	for i, developer := range detail.Data.Developers {
		res.Developers[i] = &model.Developer{
			UpdateTime: gtime.Now(),
			TaptapId:   uint(pair.TaptapId),
			Name:       developer.Name,
			Label:      developer.Label,
			Type:       developer.Type,
			Website:    developer.Website,
		}
	}
	// 种类
	for i, tag := range detail.Data.Tags {
		res.Categories[i] = &model.Category{
			UpdateTime: gtime.Now(),
			Name:       tag.Value,
		}
	}
	// 屏幕截图
	for i, s := range detail.Data.Screenshots {
		res.Screenshots[i] = &model.GameScreenshot{
			Order:      i,
			UpdateTime: gtime.Now(),
			Width:      s.Width,
			Height:     s.Height,
			MediumUrl:  s.MediumUrl,
			OriginUrl:  s.OriginalUrl,
		}
	}
	if detail.Data.Hints != nil && len(detail.Data.Hints) > 0 {
		res.Stat.Hint = detail.Data.Hints[0]
	}
	return res, nil
}
